package model.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.dao.BoardDAO;
import model.dao.BoardDAOImpl;
import model.vo.BoardCategoryVO;
import model.vo.BoardVO;
import model.vo.PageListVO;

public class BoardServiceImpl implements BoardService {
	BoardDAO boardDAO;
	public void setBoardDAO(BoardDAO boardDAO)
	{
		this.boardDAO = boardDAO;
	}
	public ArrayList<BoardCategoryVO> writePageView() {
		ArrayList<BoardCategoryVO> list = null;
		try {
			list = boardDAO.writePageView();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void write(BoardVO vo) throws SQLException {
		boardDAO.write(vo);		
	}
	@Override
	public PageListVO pageList(int page) throws SQLException {
		return boardDAO.pageList(page);
	}
	
	public BoardVO read(int no) throws SQLException{
		BoardVO vo = boardDAO.read(no);
		ArrayList<BoardCategoryVO> bclist = writePageView();
		for(int i=0 ; i<bclist.size() ; i++)
		{
			BoardCategoryVO bcvo = (BoardCategoryVO)bclist.get(i);
			if(vo.getCategory_code().equals(bcvo.getCategory_code()))
			{
				vo.setCategory_code(bcvo.getCategory_text());
				break;
			}
		}
		return vo;
	}
	
	@Override
	public void reWrite(BoardVO vo) throws SQLException{
		boardDAO.reWrite(vo);
	}
	
	public String textToCode(String text)
	{
		String code="";
		ArrayList<BoardCategoryVO> bclist = writePageView();
		for(int i=0 ; i<bclist.size() ; i++)
		{
			BoardCategoryVO bcvo = (BoardCategoryVO)bclist.get(i);
			if(text.equals(bcvo.getCategory_text()))
			{
				code = bcvo.getCategory_code();
				break;
			}
		}
		return code;
	}
	
	@Override
	public void update(BoardVO bvo) throws SQLException {
		boardDAO.update(bvo);
	}
	
	@Override
	public void delete(int titleNo) throws SQLException {
		boardDAO.delete(titleNo);
	}
	@Override
	public PageListVO searchByTitle(String searchtext, int num)
			throws SQLException {
		System.out.println("searchByTitle");
		Map map = new HashMap();
		map.put("text", searchtext);
		map.put("page", num);
		return boardDAO.searchByTitle(map);
	}
	@Override
	public PageListVO searchByWriter(String searchtext, int num)
			throws SQLException {
		System.out.println("searchByWriter");
		Map map = new HashMap();
		map.put("text", searchtext);
		map.put("page", num);
		return boardDAO.searchByWriter(map);
	}

}
